﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Infragistics.Documents.Excel;
using VNet.VisiSuite.Common.Logger;
using VNet.VisiSuite.ExcelReader.ExcelReaderFactory;
using VNet.VisiSuite.ExcelReader.Models;

namespace VNet.VisiSuite.ExcelReader.ExcelReader
{
    /// <summary>
    /// Product Reader
    /// </summary>
    public class ProductionPlanReader : IExcelReaderFactory
    {
        /// <summary>
        /// The _product name index
        /// </summary>
        private int _productNameIndex = -1;

        /// <summary>
        /// The _cell index
        /// </summary>
        private int _cellIndex = -1;

        /// <summary>
        /// The _work order index
        /// </summary>
        private int _workOrderIndex = -1;

        /// <summary>
        /// The _due date index
        /// </summary>
        private int _dueDateIndex = -1;

        /// <summary>
        /// The _planned index
        /// </summary>
        private int _plannedIndex = -1;

        /// <summary>
        /// Reads the excel sheet.
        /// </summary>
        /// <param name="workbook">The workbook.</param>
        /// <returns></returns>
        public IExcelData ReadExcelSheet(Workbook workbook)
        {
            var worksheet = workbook.Worksheets[0];
            if (worksheet == null)
            {
                FileLog.Fatal("Error: First sheet cannot be read.");
                return null;
            }

            var rowCollection = worksheet.Rows;
            var model = new ProductionPlanData
                {
                    Items = new List<ProductionPlanModelItem>(),
                };
            if (!rowCollection.Any())
            {
                return model;
            }
            var headerRow = rowCollection[0];
            GetColumnIndexes(headerRow.Cells);

            for (var i = 1; i < rowCollection.Count(); i++)
            {
                var row = worksheet.Rows[i];
                var plannned = 0;
                if (_plannedIndex >= 0)
                {
                    var pl = row.Cells[_plannedIndex].Value == null
                                    ? "0"
                                    : row.Cells[_plannedIndex].GetText().Trim();
                    int.TryParse(pl, out plannned);
                }

                var dueDate = DateTime.Today;
                if (_dueDateIndex >= 0)
                {
                    var dueDateString = string.IsNullOrEmpty(row.Cells[_dueDateIndex].GetText())
                                    ? DateTime.Today.ToString(CultureInfo.InvariantCulture)
                                    : row.Cells[_dueDateIndex].GetText();
                    DateTime.TryParse(dueDateString, out dueDate);
                }

                var item = new ProductionPlanModelItem
                {
                    CellName = (_cellIndex < 0 || row.Cells[_cellIndex].Value == null) ? null : row.Cells[_cellIndex].GetText(),
                    WorkOrder = (_workOrderIndex < 0 || row.Cells[_workOrderIndex].Value == null) ? null : row.Cells[_workOrderIndex].GetText(),
                    Sku = (_productNameIndex < 0 || row.Cells[_productNameIndex].Value == null) ? null : row.Cells[_productNameIndex].GetText(),
                    Enabled = true,
                    Planned = plannned,
                    DueDate = dueDate
                };
                model.Items.Add(item);
            }
            return model;
        }

        /// <summary>
        /// Gets the column indexes.
        /// </summary>
        /// <param name="cells">The cells.</param>
        private void GetColumnIndexes(WorksheetCellCollection cells)
        {
            for (var i = 0; i < cells.Count(); i++)
            {
                var text = cells[i].GetText();
                if (!string.IsNullOrEmpty(text))
                {
                    switch (text.ToUpper().Trim())
                    {
                        case "PRODUCT NAME":
                        case "PRODUCTNAME":
                        case "SKU":
                            _productNameIndex = i;
                            break;
                        case "CELL":
                            _cellIndex = i;
                            break;
                        case "WORKORDER":
                        case "WORK ORDER":
                            _workOrderIndex = i;
                            break;
                        case "PLANNED":
                            _plannedIndex = i;
                            break;
                        case "DUEDATE":
                        case "DUE DATE":
                            _dueDateIndex = i;
                            break;
                    }
                }
            }
        }
    }
}
